package com.jblv.frame.user.api.handler;

import com.baomidou.mybatisplus.mapper.MetaObjectHandler;
import com.jblv.frame.common.utils.UUIDGenerator;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

/**
 * @author happyonion.yangtao
 * @version 1.0
 * @title DbMetaObjectHandler
 * @projectName jblv-frame
 * @packageName com.jblv.user.api.handler
 * @description 自动填充数据库字段
 * @date Create at 2020/4/30 10:13
 * @since jdk1.8
 */
@Component
public class MyBatisPlusMetaObjectHandler extends MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        // 判断是否有createTime字段，有就自动填充，没有就跳过
        boolean have = metaObject.hasSetter("createTime");
        if (have) {
            setFieldValByName("createTime", System.currentTimeMillis(), metaObject);
            setFieldValByName("id", UUIDGenerator.getUUID(), metaObject);
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        // 判断是否已经设置好了updateTime字段，有就自动填充，没有就跳过
        Object value = getFieldValByName("updateTime", metaObject);
        if (null == value) {
            setFieldValByName("updateTime", System.currentTimeMillis(), metaObject);
            //TODO 这个后期需要改为动态获取当前用户
        }
    }
}
